#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<iostream>

#include "tools.h"
//配置酒店信息
void set_config(uint8_t floor,uint8_t room_cnt,uint8_t discount)
{
	char buf[100];
	chdir("config");
	int fd=open("hotel.conf",O_WRONLY);
    if(0>fd)
    {
      std::cout<<"文件打开失败"<<std::endl;
    }
    sprintf(buf,"%d %d %d",floor,room_cnt,discount);
    write(fd,buf,strlen(buf)+1);
    chdir("..");
    close(fd);
}
//获取酒店配置信息
void get_config(uint8_t& floor,uint8_t& room_cnt,uint8_t& discount)
{
	char buf[100];
	chdir("config");
	int fd=open("hotel.conf",O_RDONLY);
	if(0>fd)
	{
		perror("open");
		return;
	}
	read(fd,buf,sizeof(buf));
	sscanf(buf,"%d %d %d",(int*)&floor,(int*)&room_cnt,(int*)&discount);
	printf("%d %d %d\n",floor,room_cnt,discount);
	chdir("..");
	close(fd);
}
//更改折扣信息
void set_discount(uint8_t discount)
{
	uint8_t floor=0,room_cnt=0,temp=0;
	char buf[100]={};
	chdir("config");
	int fd=open("hotel.conf",O_RDONLY|O_WRONLY);
	if(0>fd)
	{
		perror("open");
		return;
	}
	read(fd,buf,sizeof(buf));
	sscanf(buf,"%d %d %d",(int*)&floor,(int*)&room_cnt,(int*)&temp);
	sprintf(buf,"%d %d %d",floor,room_cnt,discount);
	lseek(fd,0,SEEK_SET);
	write(fd,buf,strlen(buf)+1);
	chdir("..");
	close(fd);
}
